#!/usr/bin/env python
#-*- coding:utf-8 -*-


'''
'''
import re
import urllib
import urlparse
import httplib
import time
class GetSong():
    def __init__(self,songname,netnum):
        self.songname=songname
        self.netnum=netnum
        url=urllib.URLopener()
        url=url.open(self.netnum)
        data=url.read()
        m=re.compile(r'(?<=id="playicon"...).*?(?=><img)' , re.I|re.S)
        s=[]
        s=m.findall(data)
        for ss in s:
            ss=ss.replace('\'','')
            ss=ss.replace('"','')
            x=ss
        neis=x.split(',')   
        html='http://224.'+neis[2]
        html=html.replace(' ','')
        self.songname1=self.songname.decode('gb18030','replace')
        self.html=html+'.mp3'     
        '''  
        for i in range(len(self.netnum)):
            url=urllib.URLopener()
            url=url.open(self.netnum[i])
            data=url.read()
            m=re.compile(r'(?<=id="playicon"...).*?(?=><img)' , re.I|re.S)
            s=[]
            s=m.findall(data)
            for ss in s:
                ss=ss.replace('\'','')
                ss=ss.replace('"','')
                x=ss
            neis=x.split(',')   
            html='http://224.'+neis[2]
            html=html.replace(' ','')
            self.songname1=self.songname[i].decode('gb18030','replace')
            self.html=html+'.mp3'       
            try:
                url1=urllib.urlopen(html)
                parsedurl=urlparse.urlparse(html)
                httpcon=httplib.HTTPConnection(parsedurl[1])
                httpcon.request('GET', parsedurl[2])
            finally:
                print "right"
            f=open(songname1,'w+b')
            reponse=httpcon.getresponse()
            if reponse.status==200:
                 size=reponse.getheader('Content-Length')
            httpcon.close()
            url=urllib.URLopener()
            url=url.open(html)
            x=0
            while True:
                line =url.readline()
                x=x+len(line)
                f.write(line)
                if len(line)==0:
                    break
            '''
    def getHtml(self):
        return self.html 
    def getSongName(self):
        return self.songname1
           